package com.edu.service.impl;

import com.edu.component.PageBean;
import com.edu.component.QueryParam;
import com.edu.dao.UserMapper;
import com.edu.entity.User;
import com.edu.service.RoleService;
import com.edu.service.UserService;
import com.edu.utils.FileUploader;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleService roleService;
    @Override
    public User getUserByName(String name) {
        return userMapper.getUserByName(name);
    }

    @Override
    public List<User> queryAll() {
        return userMapper.queryAll();
    }

    @Override
    public PageBean listPage(QueryParam param) {
        //1.开启分页
        Page retPage = PageHelper.startPage(param.getPage(),param.getLimit(),true);
        //2.查询列表
        userMapper.queryList(param.getParam());
        //3.创建PageBean辅助类
        PageBean PB = new PageBean<>( retPage );
        return PB;
    }

    @Override
    public Integer addUser(User user) {
        //1.对用户的密码进行加密
        encodePassword(user);
        //2.调用Mapper来保存用户
        int cnt = userMapper.addUser(user);
        if ( cnt != 1 ){
            throw new RuntimeException("添加用户失败.");
        }
        //3.设置用户的角色关联
        roleService.addRoleAssociation(user.getId(), user.getRoleId());
        //4.返回用户的Id(数据库的自增ID)
        return user.getId();
    }

    //ps:对用户的密码进行加密
    private void encodePassword(User user){
        String oldPass = user.getPassword();
        BCryptPasswordEncoder ENC = new BCryptPasswordEncoder();
        String newPass = ENC.encode(oldPass);
        user.setPassword(newPass);
    }

    @Override
    public void updateHeadImgByUser(User user, MultipartFile file, String dir) {

    }

    @Override
    public void updateUser(User user) {
        //1.更新用户的单表数据
        int cnt = userMapper.updateUser(user);
        if (cnt != 1){
            throw new RuntimeException("更新用户失败.");
        }
        //2.设置用户的角色关联
        roleService.addRoleAssociation(user.getId(), user.getRoleId());
    }

    @Override
    public void deleteUser(Integer id) {

    }

    @Override
    public void deleteBatchUser(Integer[] ids) {

    }

    @Override
    public void saveUserRole(Map paramMap) {

    }

    @Override
    public void updateUserImage(Integer id, FileUploader FD) {
        try {
            User user = new User(id, FD.getFileName());
            int cnt = userMapper.updateUserImage(user);
            if (cnt!= 1){
                throw new RuntimeException("写入用户头像失败.");
            }
            FD.writeFile();    //上传文件...
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("修改用户头像失败.");
        }
    }
}
